package com.skyworth.web.admin.api;

import com.google.gson.JsonObject;
import com.skyworth.web.admin.menu.domain.MenuModel;
import com.skyworth.web.admin.menu.form.MenuForm;
import com.skyworth.web.admin.menu.service.MenuService;
import com.skyworth.web.admin.user.domain.UserModel;
import com.skyworth.web.common.controller.BaseController;
import com.skyworth.web.common.form.Paginator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 移动端菜品类别管理
 * Created by skyworth on 2016/11/10.
 */
@Controller
@RequestMapping("madmin")
public class ApiAdminMenuController extends BaseController{

	private static final Logger log = LoggerFactory.getLogger(ApiAdminMenuController.class);

	@Autowired
	private MenuService menuService;

	/*
	菜品类别列表
	 */
	@RequestMapping("/api/menu/list")
	@ResponseBody
	public Map<String, Object>  index(Paginator paginator, Model model) {
		Map<String, Object> result = new HashMap<>();
		UserModel userModel = currentUser();
		long store_id = 0;
		JsonObject params = new JsonObject();
		if (userModel != null) {
			store_id = userModel.storeId;
		}

		List<MenuModel> menuModels = menuService.listMenuByStore(store_id);
		result.put("menus", menuModels);
		return result;

	}
	/*
	菜品添加
	 */
	@RequestMapping(value = {"/api/menu/create"}, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> create(HttpServletResponse response, @ModelAttribute MenuForm menuForm, Model model) {
		Map<String, Object> result = new HashMap<>();
		if (menuForm == null) {
			result.put("errorMsg", "error");
			result.put("valid", false);
			return result;
		}
		UserModel userModel = currentUser();
		if (null == userModel) {
			result.put("valid", false);
			result.put("errorMsg", "Login first");
			return result;
		}

		long store_id = userModel.storeId;

		if (menuService.existMenu(menuForm.getName(), store_id)) {
			result.put("errorMsg", menuForm.getName() + "已存在");
			result.put("valid", false);
			return result;
		}
		menuForm.setStoreId(store_id);
		MenuModel menuModel = menuService.addMenu(menuForm);

		if (null != menuModel && menuModel.id > 0) {
			List<MenuModel> menuModels = menuService.listMenuByStore(store_id);
			result.put("valid", true);
			result.put("menus", menuModels);

		} else {
			result.put("valid", false);
			result.put("errorMsg", "添加类别失败");
		}
		return result;
	}


	/*
	修改菜品
	 */
	@RequestMapping(value = "/api/menu/{id}", method = RequestMethod.PUT)
	@ResponseBody
	public Map<String, Object> update(@PathVariable("id") long id,
									  @ModelAttribute("menu") MenuForm menuForm,
									  Model model) {
		Map<String, Object> result = new HashMap<>();
		UserModel userModel = currentUser();
		if (userModel == null) {
			result.put("valid", false);
			result.put("errorMsg", "Login first");
			return result;
		}

		if(menuService.existMenu(menuForm.getName(), id, userModel.storeId)){
			result.put("valid", false);
			result.put("errorMsg", menuForm.getName()+"已存在");
			return result;
		}
		menuForm.id = id;
		MenuModel menuModel = menuService.updateModel(menuForm);

		result.put("valid", true);
		result.put("category", menuModel);
		return result;
	}



}
